package sust.cse.ruhul.jobs1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import sust.cse.ruhul.service.JobList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Page1 extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.page1);
		
		JobCount jlist = new JobCount();
		jlist.execute(new String[]{"http://108.163.232.50:1100/LeewayWebService.asmx?op=GetAllJobs"});

		
	}

	public void onButtonClickOfPage1(final View v)
	{
		final Intent intent = new Intent(Page1.this, Page2.class);
		startActivity(intent);
	}
	
	
	public class JobCount extends AsyncTask<String, Void, String> {

		private ProgressDialog progdialod;

		// connection establish timeout (timeoutConnection)
		private int timeoutConnection = 5000;
		// socket timeout (timeoutSocket) 
		private int timeoutSocket = 45000;
		
		private byte[] buff =new byte[1024];
		private String retval="";
		
		private static final String SOAP_ACTION = "http://tempuri.org/GetAllJobs";

	    private static final String METHOD_NAME = "GetAllJobs";

	    private static final String NAMESPACE = "http://tempuri.org/";
	    private static final String URL = "http://108.163.232.50:1100/LeewayWebService.asmx";
		
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			//progdialod = ProgressDialog.show(this, "Search", "Updating",true,false);
		}
		
		
		@Override
		protected String doInBackground(String... urls) {
			
			 try {

		            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		            //request.addProperty("JobId", "3");

		            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		            envelope.dotNet=true;
		            envelope.setOutputSoapObject(request);

		            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		            androidHttpTransport.call(SOAP_ACTION, envelope);
		            
		            SoapPrimitive resultSoapPrimitive;
		            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
		                
		            String result = resultSoapPrimitive.toString();

		            retval = result;
		            
		            Log.d("return", retval);
		            
		            return retval;
		           
		        } catch (Exception e) {
		            
		       }
			 return retval;
			
		}
		
		@Override
		protected void onPostExecute(String result) {
			
			Log.d("Post Exec", "POST EXEC");
			int loopcount=0;
			
			Log.d("Post tt", result);
			//progdialod.dismiss();
			
			Log.d("Post Exec", result);
			
			try {
				JSONArray appLink = new JSONArray(result);
				loopcount = appLink.length();

			} catch (JSONException e) {
				e.printStackTrace();
			} 
	
			Button btnjobs = (Button)findViewById(R.id.btn_open_job);
			
			String extText = btnjobs.getText().toString();
			btnjobs.setText(extText+" ("+loopcount+")");
			
		}

	}
	
	
}
